package ngames.com.sushitv.action.countrycode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.R;
import ngames.com.sushitv.base.BaseAction;
import ngames.com.sushitv.tools.LogUtil;

/**
 * Created by Jeffrey on 2016/8/10.
 */
public class CountryCodeAction extends BaseAction {
    private static final String PLUS = "+";
    private ImageView mChoseCountryImv;
    private EditText mCountryCodeEdt;
//    private TextView tv_countryName;
    private GetCountryNameSort countryChangeUtil;
    private CharacterParserUtil characterParserUtil;
    private List<CountrySortModel> mAllCountryList;

    public CountryCodeAction(Activity activity, View baseView) {
        super(activity, baseView);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initView();
        initCountryList();
        setListener();
    }

    private void initView() {
        View baseView = mBaseView;
        LogUtil.d("CountryCodeAction", "initView()");
        mChoseCountryImv = (ImageView) baseView.findViewById(R.id.imv_more);
        mCountryCodeEdt = (EditText) baseView.findViewById(R.id.edt_country_code);
//        tv_countryName = (TextView) findViewById(R.id.tv_chosed_country);

        mAllCountryList = new ArrayList<CountrySortModel>();
        countryChangeUtil = new GetCountryNameSort();
        characterParserUtil = new CharacterParserUtil();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 12:
                if (resultCode == Activity.RESULT_OK) {
                    Bundle bundle = data.getExtras();
                    String countryName = bundle.getString("countryName");
                    String countryNumber = bundle.getString("countryNumber");

                    mCountryCodeEdt.setText(countryNumber);
//                    tv_countryName.setText(countryName);
                }
                break;

            default:
                break;
        }
    }

    private void initCountryList() {
        String[] countryList = mActivity.getResources().getStringArray(R.array.country_code_list_en);

        for (int i = 0, length = countryList.length; i < length; i++) {
            String[] country = countryList[i].split("\\*");
            String countryName = country[0];
            String countryNumber = country[1];
            String countrySortKey = characterParserUtil.getSelling(countryName);
            CountrySortModel countrySortModel = new CountrySortModel(countryName, countryNumber, countrySortKey);
            String sortLetter = countryChangeUtil.getSortLetterBySortKey(countrySortKey);
            if (sortLetter == null) {
                sortLetter = countryChangeUtil.getSortLetterBySortKey(countryName);
            }

            countrySortModel.sortLetters = sortLetter;
            mAllCountryList.add(countrySortModel);
        }
    }

    private void setListener() {
        mChoseCountryImv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.d("CountryCodeAction", "onClick() mChoseCountryImv");
                Intent intent = new Intent();
                intent.setClass(mActivity, CountryActivity.class);
                mActivity.startActivityForResult(intent, 12);
            }
        });

        mCountryCodeEdt.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                LogUtil.d("PostAction", "onTextChanged() s = " + s + ", start = " + start + ", before = " + before + ", count = " + count);
//                if (start == 0 && s.toString().contains(PLUS)) {
//                    mCountryCodeEdt.getEditableText().delete(start, start + count);
//                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String contentString = mCountryCodeEdt.getText().toString();
                if (!contentString.startsWith(PLUS)) {
                    int index = contentString.indexOf(PLUS);
                    if (index > 0) {
                        mCountryCodeEdt.getEditableText().delete(0, index);
                    } else {
                        mCountryCodeEdt.setText(PLUS);
                    }
                    return;
                }
                CharSequence contentSeq = mCountryCodeEdt.getText();
                // Log.i(TAG, "contentString :" + contentString.length());
                if (contentString.length() > 1) {
                    // 按照输入内容进行匹配
                    List<CountrySortModel> fileterList = (ArrayList<CountrySortModel>) countryChangeUtil.search(contentString, mAllCountryList);

//                    if (fileterList.size() > 0) {
//                        tv_countryName.setText(fileterList.get(0).countryName);
//                    } else {
//                        tv_countryName.setText("国家代码无效");
//                    }
                } else {
                    if (contentString.length() == 0) {
                        mCountryCodeEdt.setText(PLUS);
//                        tv_countryName.setText("从列表选择");
                    } else if (contentString.length() == 1 && contentString.equals("+")) {
//                        tv_countryName.setText("从列表选择");
                    }
                }

                if (contentSeq instanceof Spannable) {
                    Spannable spannable = (Spannable) contentSeq;
                    Selection.setSelection(spannable, contentSeq.length());
                }
            }
        });
    }

    public String getCountryCode() {
        String contentString = mCountryCodeEdt.getText().toString();
        return contentString.replace(PLUS, "");
    }
}
